//
//  WXPNavView.m
//  自定义导航栏
//
//  Created by wxp on 2025/2/26.
//

#import "WXPNavView.h"

@interface WXPNavView ()

@end

@implementation WXPNavView

#pragma mark - lazy
- (UIButton *)backBtn{
    if (_backBtn == nil) {
        _backBtn = [UIButton  new];
        _backBtn.widthHeight = XY(60, 44);
        [_backBtn setImage:[UIImage imageNamed:@"icon-nav-back"] forState:UIControlStateNormal];
        WEAKSELF
        [_backBtn setNoRepeatActionWithInterval:1 action:^(UIButton * _Nonnull button) {
            [NSObject clickFeedback];
            [weakSelf.backBtn addClickAnimate];
            if(weakSelf.blockPop){
                weakSelf.blockPop();
            }else{
                [Global_Nav popViewControllerAnimated:true];
            }
        }];
    }
    return  _backBtn;
}
- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = COLOR_BLACK;
        _titleLabel.font = [UIFont systemFontOfSize:18];
    }
    return _titleLabel;
}
- (WXPServiceBtn *)btnService{
    if (_btnService == nil) {
        _btnService = [WXPServiceBtn new];
    }
    return _btnService;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.width = SCREEN_WIDTH;
        self.height = [GlobalMethod getNavgationBarHeight];
        self.backgroundColor = [UIColor clearColor];
        [self addSubView];
    }
    return self;
}

//添加subview
- (void)addSubView{
    [self addSubview:self.backBtn];
    [self addSubview:self.titleLabel];
    [self addSubview:self.btnService];
    //初始化页面
    [self resetViewWithTitle:@""];
}

#pragma mark 刷新view
- (void)resetBackImageIsWhite{
    [self.backBtn setImage:[UIImage imageNamed:@"icon-face-back"] forState:UIControlStateNormal];
}
- (void)resetViewWithTitle:(NSString *)title{
    [self.titleLabel fitTitle:title variable:0];
    self.titleLabel.centerX = SCREEN_WIDTH/2;
    self.titleLabel.centerY = [GlobalMethod getNavgationBarHeight]-22;
    
    self.backBtn.leftCenterY = XY(0, self.titleLabel.centerY);
    
    self.btnService.right = SCREEN_WIDTH-LEFT_MARDIN;
    self.btnService.centerY = self.titleLabel.centerY;
}

- (void)resetNavViewWithNavAlpha:(CGFloat)alpha AndIsDefault:(BOOL)isDefault{
    self.alpha = alpha;
    
    //修改按钮的图片
    if (isDefault) {
        if ([self.backgroundColor isEqualColor:[UIColor clearColor]]) {
            return;
        }
        self.titleLabel.textColor = COLOR_BLACK;
        self.backgroundColor = [UIColor clearColor];
    }else{
        if ([self.backgroundColor isEqualColor:COLOR_PRIMARY]) {
            return;
        }
        self.titleLabel.textColor = COLOR_WHITE;
        self.backgroundColor = COLOR_PRIMARY;
    }
}

@end
